package A1_二叉树;

import java.util.Arrays;

public class LC_654最大二叉树 {
    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(){}
        TreeNode(int val){
            this.val=val;
        }
        TreeNode(int val,TreeNode left,TreeNode right){
            this.val=val;
            this.left=left;
            this.right=right;
        }

    }
    public static void main(String[] args){
        int[] nums={3,2,1,6,0,5};
        System.out.println(constructMaximumBinaryTree(nums));
    }
    public static TreeNode constructMaximumBinaryTree(int[] nums) {
        if(nums.length==1)
            return new TreeNode(nums[0]);
        int maxValue=0;
        int index=0;
        for(int i=0;i<nums.length;i++){
            if(nums[i]>maxValue){
                maxValue=nums[i];
                index=i;
            }
        }
        TreeNode node=new TreeNode(maxValue);
        if(index>0){
            int[] vec= Arrays.copyOfRange(nums,0,index);
            node.left=constructMaximumBinaryTree(vec);
        }
        if(index<nums.length-1){
            int[] vec=Arrays.copyOfRange(nums,index+1,nums.length);
            node.right=constructMaximumBinaryTree(vec);
        }
        return node;
    }
}
